﻿using System;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using MotionControl;

namespace MainFrame
{
    public partial class FormDeviceError : Form
    {
        bool totalError = false;
        bool[] isFirstError = new bool[8];
        public FormDeviceError()
        {
            InitializeComponent();
        }


        private void FormDeviceError_Load(object sender, EventArgs e)
        {
            //HOperation.SetOut(GlobalMotion.mainCard.m_CardID, (ushort)6, 1);//急停停转盘
            GlobalVar.isPause = true;
         
            GlobalVar.isEstop = true;
            GlobalVar.isAutoRun = false;
       
            GlobalVar.isAllHome = false;

            GlobalVar.errorTips[(int)e_errorType.softEstop].haveInfo = true;
            GlobalVar.errorTips[(int)e_errorType.softEstop].Info = "急停按下";
            GlobalMotion.Card_1.StopAllAxis();
            timer_Refresh.Enabled = true;
            clearInfo();
            updateDisplay();

        }

        private void updateDisplay()
        {
            for(int i=0;i< GlobalVar.errorTips.Length;i++)
            {
                if(!GlobalVar.errorTips[i].haveInfo)
                    GlobalVar.errorTips[i].Info = string.Empty;
                switch (i)
                {
                    case (int)e_errorType.axisError:
                        Invoke(new Action(() => lbl_AxisError.Text = GlobalVar.errorTips[i].Info)) ;
                        break;
                    case (int)e_errorType.cylinderError:
                        Invoke(new Action(() => lbl_CylinderError.Text = GlobalVar.errorTips[i].Info));
                        break;
                    case (int)e_errorType.homeError:
                        Invoke(new Action(() => lbl_HomeError.Text = GlobalVar.errorTips[i].Info));
                        break;
                    case (int)e_errorType.imageError:
                        Invoke(new Action(() => lbl_CameraError.Text = GlobalVar.errorTips[i].Info));
                        break;
                    case (int)e_errorType.softEstop:
                        Invoke(new Action(() => lbl_ManualAbruptStop.Text = GlobalVar.errorTips[i].Info));
                        break;
                }
            }
        }
        public void SetFalse()
        {
            for(int i=0; i<GlobalVar.errorTips.Length;i++)
            {
                GlobalVar.errorTips[i].Info = string.Empty;
                GlobalVar.errorTips[i].haveInfo = false;
            }
        }
        private void clearInfo()
        {
            totalError = false;
            lbl_AxisError.Text = string.Empty;
            lbl_CylinderError.Text = string.Empty;
            lbl_HomeError.Text = string.Empty;
            lbl_CameraError.Text = string.Empty;
            lbl_ManualAbruptStop.Text = string.Empty;
        }
        private void timer_Refresh_Tick(object sender, EventArgs e)
        {
            Stopwatch cameraErrorSW = new Stopwatch();
            updateDisplay();
            //SetFalse();
            totalError = false;
            for (int i=0;i<GlobalVar.errorTips.Length;i++)
            {
                totalError = GlobalVar.errorTips[i].haveInfo || totalError;
            }
            if (totalError)
            {
                //clearInfo();
                updateDisplay();
            }
            else
            {
                this.Close();

            }
        }
        private void clearErrorInfo()
        {
            GlobalVar.errorTips[(int)e_errorType.axisError].Info =string.Empty;
            GlobalVar.errorTips[(int)e_errorType.cylinderError].Info = string.Empty;
            GlobalVar.errorTips[(int)e_errorType.homeError].Info = string.Empty;
            GlobalVar.errorTips[(int)e_errorType.imageError].Info = string.Empty;
            GlobalVar.errorTips[(int)e_errorType.softEstop].Info = string.Empty;
            GlobalVar.errorTips[(int)e_errorType.weldError].Info = string.Empty;
        }

        private void FormDeviceError_FormClosing(object sender, FormClosingEventArgs e)
        {
            clearInfo();
            SetFalse();
            GlobalVar.isEstop = false;
        }
    }
}
